package Panel;

import Bean.Basic;
import Bean.Contact;
import Dao.BasicDao;
import Dao.ContactDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

public class PersonnelManagementPanel extends JPanel {
    //人员管理测试方法
//    public static void main(String[] args) {
//       PersonnelManagementPanel f= new PersonnelManagementPanel();
//       f. showAddModel();
//    }
    //控件
    private JPanel r = new JPanel();
    private JLabel pho;
    private JLabel phoff;
    private JLabel fax;
    private JLabel mail;
    private JLabel address;
    private JTextField phoInp;
    private JTextField phoffInp;
    private JTextField faxInp;
    private JTextField mailInp;
    private JTextField addressInp;
    private JCheckBox age;
    private ButtonGroup sex;
    private JRadioButton male;
    private JRadioButton female;
    private JButton add;
    private JButton mod;
    private JButton del;
    private JLabel tit;
    private List<Basic> basicMessage;
    private List<Contact> contactMessage;
    private String[] items;
    private String[] items2;
    private JComboBox depCb;
    private JComboBox nameCb;
    private JFrame  modelJpanel = new JFrame("填加员工信息窗体");
    private JComboBox dep;
    private JComboBox title;
    private JTextField sec_fax;
    private JTextField sec_mail;
    private JTextField sec_address;
    int flag = 0;


    public PersonnelManagementPanel() {
        this.setLayout(null);
        this.setBounds(0, 100, 680, 365);
        this.setBackground(Color.gray);
        r.setLayout(null);
        r.setBackground(Color.yellow);
        r.setBounds(130, 100, 480, 220);
        addTopPanel();
        addLeftPanel();
        addRightPanel(2);
        this.add(r);
        this.setVisible(true);
    }

    public void addTopPanel() {
        JPanel t = new JPanel();
        t.setBounds(20, 20, 600, 50);
        JLabel dep = new JLabel("部门:");
        JLabel name = new JLabel("姓名:");
        //获取基本数据
        basicMessage = BasicDao.getBasicMessage();
        //获取联系数据
        contactMessage = ContactDao.getContactMessage();
        items = new String[basicMessage.size()];
        items2 = new String[basicMessage.size()];
        for (int i = 0; i < items.length; i++) {
            items[i] = basicMessage.get(i).getDep();
            items2[i] = basicMessage.get(i).getName();
        }
        depCb = new JComboBox(items);
        nameCb = new JComboBox(items2);
        nameCb.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent itemEvent) {
                if(itemEvent.getStateChange() == ItemEvent.SELECTED){
                    if("基本信息".equals(tit.getText())){
                        addDataToBasic();
                    }else if("联系方式".equals(tit.getText())) {
                        addDataTocontact();
                    }
                }
            }
        });

        dep.setBounds(100, 30, 100, 20);
        depCb.setBounds(150, 30, 100, 20);
        name.setBounds(300, 30, 100, 20);
        nameCb.setBounds(350, 30, 100, 20);
//        这里应该放在t上
        this.add(nameCb);
        this.add(name);
        this.add(depCb);
        this.add(dep);
        this.add(t);

    }

    public void addLeftPanel() {
        JPanel l = new JPanel();
        l.setLayout(null);
        l.setBounds(20, 50, 100, 500);
        l.setBackground(Color.gray);
        JLabel tit = new JLabel("员工信息");
        tit.setBounds(20, 30, 100, 20);
        JPanel inner = new JPanel();
        inner.setBackground(Color.white);
        inner.setBounds(20, 50, 100, 200);
        JButton basic = new JButton("基本信息");
        JButton contact = new JButton("联系方式");
        basic.setBounds(20, 50, 80, 30);
        contact.setBounds(20, 80, 80, 30);
        basic.setMargin(new Insets(0, 0, 0, 0));
        contact.setMargin(new Insets(0, 0, 0, 0));
        basic.setBackground(new Color(57, 105, 140));
        basic.setForeground(Color.white);
        contact.setBackground(Color.white);
        basic.setBorder(BorderFactory.createLineBorder(Color.white));
        contact.setBorder(BorderFactory.createLineBorder(Color.white));

        //对应按钮操作
        basic.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                basic.setBackground(new Color(57, 105, 140));
                basic.setForeground(Color.white);
                contact.setBackground(Color.white);
                contact.setForeground(Color.black);
                addRightPanel(2);
                reDraw(2);
            }
        });
        contact.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                contact.setBackground(new Color(57, 105, 140));
                contact.setForeground(Color.white);
                basic.setBackground(Color.white);
                basic.setForeground(Color.black);
                addRightPanel(1);
                reDraw(1);
            }
        });

        l.add(contact);
        l.add(basic);
        l.add(inner);
        l.add(tit);
        this.add(l);
    }

    public void addRightPanel(int where) {
        add = new JButton("添加");
        mod = new JButton("修改");
        del = new JButton("删除");
        tit = new JLabel();
        tit.setBounds(0, 0, 100, 20);
        add.setBounds(50, 180, 80, 20);
        mod.setBounds(150, 180, 80, 20);
        del.setBounds(250, 180, 80, 20);
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showAddModel();
            }
        });

        mod.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModModel();
            }
        });

        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int i = JOptionPane.showConfirmDialog(modelJpanel, "确认要删除"+nameCb.getSelectedItem()+"吗?");
                if(i==0){
                    //执行确认操作
                    boolean b = BasicDao.delInfo(nameCb.getSelectedIndex()+1);
                    if(b){
                        JOptionPane.showMessageDialog(modelJpanel,"删除成功");
                    }else{
                        JOptionPane.showMessageDialog(modelJpanel,"删除失败 请重试");

                    }
                }
            }
        });
        if (where == 1) {
            tit.setText("联系方式");
            pho = new JLabel("手机:");
            phoff = new JLabel("办公电话:");
            fax = new JLabel("传真:");
            mail = new JLabel("邮箱:");
            address = new JLabel("家庭住址:");
            phoInp = new JTextField();
            phoffInp = new JTextField();
            faxInp = new JTextField();
            mailInp = new JTextField();
            addressInp = new JTextField();
            pho.setBounds(20, 30, 100, 20);
            fax.setBounds(20, 80, 100, 20);
            address.setBounds(0, 130, 100, 20);
            phoInp.setBounds(50, 30, 150, 20);
            faxInp.setBounds(50, 80, 150, 20);
            addressInp.setBounds(50, 130, 150, 20);
            phoff.setBounds(210, 30, 100, 20);
            mail.setBounds(230, 80, 100, 20);
            phoffInp.setBounds(280, 30, 150, 20);
            mailInp.setBounds(280, 80, 150, 20);
            addDataTocontact();
        } else if (where == 2) {
            tit.setText("基本信息");
            pho = new JLabel("姓名:");
            phoff = new JLabel("年龄:");
            fax = new JLabel("性别:");
            mail = new JLabel("部门:");
            address = new JLabel("职务:");
            phoInp = new JTextField();
            phoffInp = new JTextField();
            male = new JRadioButton("男");
            female = new JRadioButton("女");
            male.setSelected(true);
            male.setBounds(50, 80, 50, 20);
            female.setBounds(100, 80, 50, 20);
            male.setOpaque(false);
            female.setOpaque(false);
            sex = new ButtonGroup();
            sex.add(male);
            sex.add(female);
            mailInp = new JTextField();
            addressInp = new JTextField();
            pho.setBounds(20, 30, 100, 20);
            fax.setBounds(20, 80, 100, 20);
            address.setBounds(20, 130, 100, 20);
            phoInp.setBounds(50, 30, 150, 20);
            addressInp.setBounds(50, 130, 150, 20);
            phoff.setBounds(230, 30, 100, 20);
            mail.setBounds(230, 80, 100, 20);
            phoffInp.setBounds(280, 30, 150, 20);
            mailInp.setBounds(280, 80, 150, 20);
            r.add(male);
            r.add(female);
            addDataToBasic();
            addViews();

        }
        r.add(add);
        r.add(mod);
        r.add(del);

    }

  public void   reDraw(int i){
        if(i==2){
            r.removeAll();
            r.setVisible(false);
            r.add(male);
            r.add(female);
            addViews();
            r.setVisible(true);
        }else{
            r.removeAll();
            r.setVisible(false);
            r.add(faxInp);
            addViews();
            r.setVisible(true);
        }
    }

    //添加相同的控件
    public void addViews(){
        r.add(mailInp);
        r.add(phoffInp);
        r.add(mail);
        r.add(phoff);
        r.add(phoInp);
        r.add(addressInp);
        r.add(fax);
        r.add(address);
        r.add(pho);
        r.add(add);
        r.add(mod);
        r.add(del);
        r.add(tit);
    }

    public void addDataToBasic(){
        //渲染数据
//            获取用户id
        int id = nameCb.getSelectedIndex();
        Basic basic = basicMessage.get(id);
        phoInp.setText(basic.getName());
        phoffInp.setText(String.valueOf(basic.getAge()));
        if (basic.getSex() == 1) {
            male.setSelected(true);
        } else {
            female.setSelected(true);
        }
        mailInp.setText(basic.getDep());
        addressInp.setText(basic.getHeadship());
    }

    public void addDataTocontact(){
        int id = nameCb.getSelectedIndex();
        Contact contact = contactMessage.get(id);
        phoInp.setText(contact.getTel());
        phoffInp.setText(String.valueOf(contact.getTelOff()));
        faxInp.setText(String.valueOf(contact.getFax()));
        mailInp.setText(contact.getMail());
        addressInp.setText(contact.getAddress());
    }

  public void  showAddModel(){
      modelJpanel.setLayout(null);
      modelJpanel.setBackground(Color.RED);
      modelJpanel.setBounds(450,300,600,300);
      JButton fir = new JButton("基本信息");
      JButton sec = new JButton("联系资料");
      JButton add = new JButton("添加");
      JButton del = new JButton("关闭");
      add.setBounds(50, 180, 80, 20);
      del.setBounds(180, 180, 80, 20);
      fir.setBounds(0,0,85,20);
      sec.setBounds(85,0,85,20);
      fir.setBackground(new Color(156,182,206));
      tit.setText("基本信息");
      pho = new JLabel("姓名:");
      phoff = new JLabel("年龄:");
      fax = new JLabel("性别:");
      mail = new JLabel("部门:");
      address = new JLabel("职务:");
      phoInp = new JTextField();
      phoffInp = new JTextField();
      male = new JRadioButton("男");
      female = new JRadioButton("女");
      male.setSelected(true);
      male.setBounds(50, 80, 50, 20);
      female.setBounds(100, 80, 50, 20);
      male.setOpaque(false);
      female.setOpaque(false);
      sex = new ButtonGroup();
      sex.add(male);
      sex.add(female);
      pho.setBounds(20, 30, 100, 20);
      fax.setBounds(20, 80, 100, 20);
      address.setBounds(20, 130, 100, 20);
      phoInp.setBounds(50, 30, 150, 20);
      phoff.setBounds(230, 30, 100, 20);
      mail.setBounds(230, 80, 100, 20);
      phoffInp.setBounds(280, 30, 150, 20);
      dep = new JComboBox(new String[]{"收银部门","销售部门"});
      title = new JComboBox(new String[]{"总经理","售货员","收银员"});
//      addressInp.setBounds(50, 130, 150, 20);
//      mailInp.setBounds(280, 80, 150, 20);
      dep.setBounds(50, 130, 150, 20);
      title.setBounds(280, 80, 150, 20);
      modelJpanel.add(male);
      modelJpanel.add(female);
      modelJpanel.add(phoffInp);
      modelJpanel.add(mail);
      modelJpanel.add(phoff);
      modelJpanel.add(phoInp);
      modelJpanel.add(fax);
      modelJpanel.add(address);
      modelJpanel.add(pho);
      modelJpanel.add(add);
      modelJpanel.add(dep);
      modelJpanel.add(title);
      del.setText("关闭");
      modelJpanel.add(del);
      add.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent actionEvent) {
              if(flag==1){
                  Contact contact = new Contact();
                  try{
                      contact.setTel(phoInp.getText());
                      contact.setTelOff(phoffInp.getText());
                      contact.setFax(sec_fax.getText());
                      contact.setAddress(sec_address.getText());
                      contact.setMail(sec_mail.getText());
                  }catch (NumberFormatException e){
                      JOptionPane.showMessageDialog(modelJpanel,"数据类型输入错误,请重试");
                      return;
                  }
                  boolean b1 = addInfo("联系资料", contact);
                  if(b1){
                      JOptionPane.showMessageDialog(modelJpanel,"添加成功");
                  }else{
                      JOptionPane.showMessageDialog(modelJpanel,"添加失败 请重试");
                  }
              }else{
                  Basic basic = new Basic();
                  try{
                      basic.setName(phoInp.getText());
                      basic.setAge(Integer.parseInt(phoffInp.getText()));
                      if(male.isSelected()){
                          basic.setSex(1);
                      }else {
                          basic.setSex(0);
                      }
                      basic.setDep(String.valueOf(dep.getSelectedItem()));
                      basic.setHeadship(String.valueOf(title.getSelectedItem()));
                  }catch (NumberFormatException e){
                      JOptionPane.showMessageDialog(modelJpanel,"数据类型输入错误,请重试");
                      return;
                  }
                  boolean basic1 = addInfo("基本信息", basic);
                  if(basic1){
                      JOptionPane.showMessageDialog(modelJpanel,"添加成功");
                  }else{
                      JOptionPane.showMessageDialog(modelJpanel,"添加失败 请重试");
                  }
              }

          }
      });
      del.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent actionEvent) {
              modelJpanel.dispose();
              addViews();
          }
      });
      fir.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent actionEvent) {
              flag = 0;
                fir.setBackground(new Color(156,182,206));
                sec.setBackground(new Color(214,227,240));
              pho.setText("姓名:");
              phoff.setText("年龄:");
              fax.setText("性别:");
              mail.setText("部门:");
              address.setText("职务:");
              modelJpanel.setVisible(false);
              modelJpanel.add(male);
              modelJpanel.add(female);
              modelJpanel.add(dep);
              modelJpanel.add(title);
              modelJpanel.remove(sec_address);
              modelJpanel.remove(sec_fax);
              modelJpanel.remove(sec_mail);
              modelJpanel.setVisible(true);

          }
      });

      sec.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent actionEvent) {
              flag = 1;
              sec.setBackground(new Color(156,182,206));
              fir.setBackground(new Color(214,227,240));
              pho.setText("手机:");
              phoff.setText("办公电话:");
              fax.setText("传真:");
              mail.setText("邮箱:");
              address.setText("住址:");
             sec_fax = new JTextField();
               sec_mail = new JTextField();
               sec_address = new JTextField();
              sec_fax.setBounds(50,80,150,20);
              sec_mail.setBounds(50, 130, 150, 20);
              sec_address.setBounds(280, 80, 150, 20);
              modelJpanel.setVisible(false);
              modelJpanel.remove(male);
              modelJpanel.remove(female);
              modelJpanel.remove(dep);
              modelJpanel.remove(title);
              modelJpanel.add(sec_address);
              modelJpanel.add(sec_fax);
              modelJpanel.add(sec_mail);
              modelJpanel.setVisible(true);
          }
      });
      modelJpanel.add(fir);
      modelJpanel.add(sec);
      modelJpanel.setVisible(true);
    }

    //修改模态框
    public  void showModModel(){

    }

    public boolean addInfo(String type,Object basic){
            Boolean b = null;
            if("基本信息".equals(type)){
                System.out.println(b);
                b = BasicDao.addBasicMessage((Basic) basic);
            }else if("联系资料".equals(type)){
                b = ContactDao.addContactMessage((Contact) basic);
            }
            if(b!=null && b ){
            return true;
             }
        return false;



    }
}
